﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_DanhMucNganhNghe_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
    Dim level As String = -1
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidID.Value = Request.QueryString("Id")
                ShowData()
                getParent()
            End If
        End If
    End Sub
    Protected Sub getParent()
        Using data As New VSEntities
            Dim p As List(Of DanhMucNganhNghe) = (From q In data.DanhMucNganhNghes Select q).ToList
            With ddlParentId
                .DataSource = p
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
                .Items.Insert(0, New ListItem("-Chọn ngành cấp cha-", "0"))
            End With
            RecursiveFillTree(p, 0)
        End Using
    End Sub
    Private Sub RecursiveFillTree(ByVal dtParent As List(Of DanhMucNganhNghe), ByVal parentID As Integer)
        level += 1
        'on the each call level increment 1
        Dim appender As New StringBuilder()

        For j As Integer = 0 To level - 1

            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;")
        Next
        If level > 0 Then
            appender.Append("|__")
        End If

        Dim data As New VSEntities
        Dim dv As List(Of DanhMucNganhNghe) = (From q In data.DanhMucNganhNghes Where q.ParentId = parentID Select q).ToList
        Dim i As Integer

        If dv.Count > 0 Then
            For i = 0 To dv.Count - 1
                ddlParentId.Items.Add(New ListItem(Server.HtmlDecode(appender.ToString() + dv.Item(i).Name.ToString()), dv.Item(i).Id.ToString()))
                RecursiveFillTree(dtParent, Integer.Parse(dv.Item(i).Id.ToString()))
            Next
        End If

        level -= 1
        'on the each function end level will decrement by 1
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As DanhMucNganhNghe = (From q In data.DanhMucNganhNghes Where q.Id = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                txtCode.Text = IIf(IsNothing(p.Code) = True, "", p.Code)
                txtCap1.Text = IIf(IsNothing(p.CAP1) = True, "", p.CAP1)
                txtCap2.Text = IIf(IsNothing(p.CAP2) = True, "", p.CAP2)
                txtCap3.Text = IIf(IsNothing(p.CAP3) = True, "", p.CAP3)
                txtCap4.Text = IIf(IsNothing(p.CAP4) = True, "", p.CAP4)
                txtCap5.Text = IIf(IsNothing(p.CAP5) = True, "", p.CAP5)
                txtName.Text = IIf(IsNothing(p.Name) = True, "", p.Name)
                If Not p.ParentId Is Nothing AndAlso p.ParentId <> 0 Then
                    ddlParentId.SelectedValue = p.ParentId 'Cap cha
                Else
                    ddlParentId.SelectedIndex = -1
                End If
            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim p As DanhMucNganhNghe = (From q In data.DanhMucNganhNghes Where q.Id = hidID.Value).SingleOrDefault
            If Not p Is Nothing Then
                Dim _check = (From q In data.DanhMucNganhNghes Where q.Id <> hidID.Value And q.Code = txtCode.Text.Trim() And q.Name = txtName.Text.Trim() Select q).FirstOrDefault
                If _check Is Nothing Then
                    Try
                        'p.Id = hidID.Value
                        p.Code = txtCode.Text.Trim()
                        p.CAP1 = txtCap1.Text.Trim()
                        p.CAP2 = txtCap2.Text.Trim()
                        p.CAP3 = txtCap3.Text.Trim()
                        p.CAP4 = txtCap4.Text.Trim()
                        p.CAP5 = txtCap5.Text.Trim()
                        p.Name = txtName.Text.Trim()
                        p.ParentId = ddlParentId.SelectedValue
                        data.SaveChanges()
                        Insert_App_Log("Insert DanhMucNganhNghe:" & txtName.Text.Trim & "", Function_Name.DanhMucNganhNghe, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                        Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/DanhMucNganhNghe/List.aspx';", Me.Page, True)
                    Catch ex As Exception
                        log4net.Config.XmlConfigurator.Configure()
                        log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                        Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
                    End Try
                Else
                    Excute_Javascript("Alertbox('Dữ liệu tồn tại trong hệ thống.');", Me.Page, True)
                End If

            End If

        End Using
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ShowData()
    End Sub
    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
